package bite;

import java.util.Scanner;

/**
 * Creared with IntelliJ IDEA.
 * Description:【Rational Arithmetic (20)】For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference,
 * product and quotient.
 * User:yxd
 * Date:2022-06-13
 * Time:16:41
 */
//有理数运算
class Rational{

    public long numerator;//分子
    public long denominator;//分模
    public long integer;//整数部分
    public long totalNumerator;//参与运算的分子,就是不分解的
    public boolean isNegtive = false;//是不是负数
    public boolean isZero = false;//这个数是不是零
    public boolean Nan = false;//分母是不是零

    public static long parseNumerator(String s) {
        //分子部分
        return Long.parseLong(s.substring(0,s.indexOf('/')));
    }

    public static long parseDenominator(String s) {
        //分母部分
        return Long.parseLong(s.substring(s.indexOf('/')+1,s.length()));
    }

    public Rational(long n, long d) {
        if (n == 0) {
            //分子为0
            isZero = true;
        }
        if (d == 0) {
            //分母为0
            Nan = true;
            return;
        }
        //符号,是不是负数
        if (n < 0) {
            isNegtive = !isNegtive;
        }
        if (d < 0) {
            isNegtive = !isNegtive;
        }
        //整数部分
        integer = n / d;
        //分子部分,应该放入结果的分子
        numerator = n - integer * d;
        //分母
        denominator = d;
        //化成最简形式
        if (numerator > 1 || numerator < -1) {
            //保证是正数
            long gcd = calGcd(Math.abs(numerator),Math.abs(denominator));
            if (gcd > 0) {
                numerator /= gcd;
                denominator /= gcd;
            }
        }
        totalNumerator = numerator + integer * denominator;
    }

    //求最大公约数
    private long calGcd(long a, long b) {
        if (b == 0) return a;
        return calGcd(b,a % b);
    }
    //加
    public Rational add(Rational r) {
        long d = denominator * r.denominator;
        long n = totalNumerator * r.denominator + r.totalNumerator * denominator;
        return new Rational(n,d);
    }

    //减
    public Rational sub(Rational r) {
        long d = denominator * r.denominator;
        long n = totalNumerator * r.denominator - r.totalNumerator * denominator;
        return new Rational(n,d);
    }

    //乘
    public Rational mul(Rational r) {
        long d = denominator * r.denominator;
        long n = totalNumerator * r.totalNumerator;
        return new Rational(n,d);
    }


    //除
    public Rational div(Rational r) {
        long d = denominator * r.totalNumerator;
        long n = totalNumerator * r.denominator;
        return new Rational(n,d);
    }
    //重写toString,保证输出正确
    @Override
    public String toString() {
        if (Nan) {
            //分母有0
            return "Inf";
        }
        if (isZero) {
            //结果为0
            return "0";
        }
        StringBuffer s = new StringBuffer();
        if (isNegtive) {
            //结果为负数
            s.append("(-");
        }
        if (integer != 0) {
            //有整数部分,就加上整数部分,符号在前面已经判断过了,此处全部转换为正数就好
            s.append(Math.abs(integer));
            if (0 != numerator) {
                s.append(' ');
            }
        }
        if (0 != numerator) {
            //分子不为零,表示有分子部分
            s.append(Math.abs(numerator));
            s.append('/');
            s.append(Math.abs(denominator));
        }
        if (isNegtive) {
            //补全负数部分
            s.append(')');
        }
        return s.toString();
    }

}
public class Day36_1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            String s = input.next();
            long n = Rational.parseNumerator(s);
            long d = Rational.parseDenominator(s);
            Rational r1 = new Rational(n,d);
            s = input.next();
            n = Rational.parseNumerator(s);
            d = Rational.parseDenominator(s);
            Rational r2 = new Rational(n,d);

            System.out.println(r1 +" + "+r2+ " = "+r1.add(r2));
            System.out.println(r1 +" - "+r2+ " = "+r1.sub(r2));
            System.out.println(r1 +" * "+r2+ " = "+r1.mul(r2));
            System.out.println(r1 +" / "+r2+ " = "+r1.div(r2));
        }
    }




    public static void main1(String[] args) {
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        String[] arr = s.split(" ");
        String str1 = arr[0];//第一个有理数
        String str2 = arr[1];//第二个有理数
        boolean flg1 = true;
        boolean flg2 = true;
        if(str1.charAt(0) == '-'){
            flg1 = false;
            str1 = str1.substring(1,str1.length());
        }
        //是负数
        if(str2.charAt(0) == '-'){
            flg2 = false;
            str2 = str2.substring(1,str2.length());
        }
        String[] nums1 = str1.split("/");
        int a1 = Integer.parseInt(nums1[0]);
        int b1 = Integer.parseInt(nums1[1]);
        String[] nums2 = str2.split("/");
        int a2 = Integer.parseInt(nums2[0]);
        int b2 = Integer.parseInt(nums2[1]);
        //加法
        StringBuilder stringBuilder = new StringBuilder();
        int num = a1 / b1;
        int sp = a1 % b1;
        if(!flg1){
            stringBuilder.append("(");
            stringBuilder.append("-");
            if(num == 0){
                if(sp == 0){
                    stringBuilder.append(num);
                }
            }else{
                stringBuilder.append(num);
                if(sp != 0){
                    stringBuilder.append(" ");
                }
            }
            if(sp != 0){
                int z1 = y(sp,b1);
                stringBuilder.append(sp / z1);
                stringBuilder.append("/");
                stringBuilder.append(b1 / z1);
            }
            stringBuilder.append(")");
        }else{
            if(num == 0){
                if(sp == 0){
                    stringBuilder.append(num);
                }
            }else{
                stringBuilder.append(num);
                if(sp != 0){
                    stringBuilder.append(" ");
                }
            }
            if(sp != 0){
                int z1 = y(sp,b1);
                stringBuilder.append(sp / z1);
                stringBuilder.append("/");
                stringBuilder.append(b1 / z1);
            }
        }
        stringBuilder.append(" ");
        stringBuilder.append("+");
        stringBuilder.append(" ");
        if(!flg2){
            stringBuilder.append("(");
            stringBuilder.append("-");
            int num2 = a2 / b2;
            int sp2 = a2 % b2;
            if(num2 == 0){
                if(sp2 == 0){
                    stringBuilder.append(num2);
                }
            }else{
                stringBuilder.append(num2);
                if(sp2 != 0){
                    stringBuilder.append(" ");
                }
            }
            if(sp2 != 0){
                int zz = y(sp2,b2);
                stringBuilder.append(sp2 / zz);
                stringBuilder.append("/");
                stringBuilder.append(b2 / zz);
            }
            stringBuilder.append(")");
        }else{
            int num2 = a2 / b2;
            int sp2 = a2 % b2;
            if(num2 == 0){
                if(sp2 == 0){
                    stringBuilder.append(num2);
                }
            }else{
                stringBuilder.append(num2);
                if(sp2 != 0){
                    stringBuilder.append(" ");
                }
            }
            if(sp2 != 0){
                int zz = y(sp2,b2);
                stringBuilder.append(sp2 / zz);
                stringBuilder.append("/");
                stringBuilder.append(b2 / zz);
            }
        }
        stringBuilder.append(" ");
        stringBuilder.append("=");//上面就全部是前面的部分
        stringBuilder.append(" ");
        int sum1 = a1 * b2 * (flg1 ? 1 : -1) + a2 * b1 * (flg2 ? 1 : -1);
        boolean f = true;
        if(sum1 < 0){
            f = false;
            sum1 = -sum1;
        }
        int sum2 = b1 * b2;
        if(sum2 < 0){
            sum2 = -sum2;
        }
        int top = sum1 / sum2;
        int mu = sum1 % sum2;
        int z = y(mu,sum2);
        if(!f){
            stringBuilder.append("(");
            stringBuilder.append("-");
            if(top == 0){
                if(mu == 0){
                    stringBuilder.append(top);
                }
            }else{
                stringBuilder.append(top);
                if(mu != 0){
                    stringBuilder.append(" ");
                }
            }
            if(mu != 0){
                stringBuilder.append(mu / z);
                stringBuilder.append("/");
                stringBuilder.append(sum2 / z);
            }
            stringBuilder.append(")");
        }else{
            if(top == 0){
                if(mu == 0){
                    stringBuilder.append(top);
                }
            }else{
                stringBuilder.append(top);
                if(mu != 0){
                    stringBuilder.append(" ");
                }
            }
            if(mu != 0){
                stringBuilder.append(mu / z);
                stringBuilder.append("/");
                stringBuilder.append(sum2 / z);
            }
        }
        String add = stringBuilder.toString();
        System.out.println(add);
        //减法
        stringBuilder.delete(stringBuilder.indexOf("=") + 2,stringBuilder.length());
        int index= stringBuilder.indexOf("+");
        stringBuilder.setCharAt(index,'-');
        sum1 = a1 * b2 * (flg1 ? 1 : -1) - a2 * b1 * (flg2 ? 1 : -1);
        f = true;
        if(sum1 < 0){
            f = false;
            sum1 = -sum1;
        }
        sum2 = b1 * b2;
        if(sum2 < 0){
            sum2 = -sum2;
        }
        top = sum1 / sum2;
        mu = sum1 % sum2;
        z = y(mu,sum2);
        if(!f){
            stringBuilder.append("(");
            stringBuilder.append("-");
            if(top == 0){
                if(mu == 0){
                    stringBuilder.append(top);
                }
            }else{
                stringBuilder.append(top);
                if(mu != 0){
                    stringBuilder.append(" ");
                }
            }
            if(mu != 0){
                stringBuilder.append(mu / z);
                stringBuilder.append("/");
                stringBuilder.append(sum2 / z);
            }
            stringBuilder.append(")");
        }else{
            if(top == 0){
                if(mu == 0){
                    stringBuilder.append(top);
                }
            }else{
                stringBuilder.append(top);
                if(mu != 0){
                    stringBuilder.append(" ");
                }
            }
            if(mu != 0){
                stringBuilder.append(mu / z);
                stringBuilder.append("/");
                stringBuilder.append(sum2 / z);
            }
        }
        String subtract = stringBuilder.toString();
        System.out.println(subtract);
        //乘法
        stringBuilder.delete(stringBuilder.indexOf("=") + 2,stringBuilder.length());
        stringBuilder.setCharAt(index,'*');
        sum1 = a1 * a2 * (flg1 ? 1 : -1) * (flg2 ? 1 : -1);
        f = true;
        if(sum1 < 0){
            f = false;
            sum1 = -sum1;
        }
        sum2 = b1 * b2;
        if(sum2 < 0){
            sum2 = -sum2;
        }
        top = sum1 / sum2;
        mu = sum1 % sum2;
        z = y(mu,sum2);
        if(!f){
            stringBuilder.append("(");
            stringBuilder.append("-");
            if(top == 0){
                if(mu == 0){
                    stringBuilder.append(top);
                }
            }else{
                stringBuilder.append(top);
                if(mu != 0){
                    stringBuilder.append(" ");
                }
            }
            if(mu != 0){
                stringBuilder.append(mu / z);
                stringBuilder.append("/");
                stringBuilder.append(sum2 / z);
            }
            stringBuilder.append(")");
        }else{
            if(top == 0){
                if(mu == 0){
                    stringBuilder.append(top);
                }
            }else{
                stringBuilder.append(top);
                if(mu != 0){
                    stringBuilder.append(" ");
                }
            }
            if(mu != 0){
                stringBuilder.append(mu / z);
                stringBuilder.append("/");
                stringBuilder.append(sum2 / z);
            }
        }
        String x = stringBuilder.toString();
        System.out.println(x);
        //除法
        stringBuilder.delete(stringBuilder.indexOf("=") + 2,stringBuilder.length());
        stringBuilder.setCharAt(index,'/');
        sum1 = a1 * b2 * (flg1 ? 1 : -1) * (flg2 ? 1 : -1);
        f = true;
        if(sum1 < 0){
            f = false;
            sum1 = -sum1;
        }
        sum2 = b1 * a2;
        if(sum2 < 0){
            sum2 = -sum2;
        }
        if(sum2 == 0){
            stringBuilder.append("Inf");
        }else{
            top = sum1 / sum2;
            mu = sum1 % sum2;
            z = y(mu,sum2);
            if(!f){
                stringBuilder.append("(");
                stringBuilder.append("-");
                if(top == 0){
                    if(mu == 0){
                        stringBuilder.append(top);
                    }
                }else{
                    stringBuilder.append(top);
                    if(mu != 0){
                        stringBuilder.append(" ");
                    }
                }
                if(mu != 0){
                    stringBuilder.append(mu / z);
                    stringBuilder.append("/");
                    stringBuilder.append(sum2 / z);
                }
                stringBuilder.append(")");
            }else{
                if(top == 0){
                    if(mu == 0){
                        stringBuilder.append(top);
                    }
                }else{
                    stringBuilder.append(top);
                    if(mu != 0){
                        stringBuilder.append(" ");
                    }
                }
                if(mu != 0){
                    stringBuilder.append(mu / z);
                    stringBuilder.append("/");
                    stringBuilder.append(sum2 / z);
                }
            }
        }
        String y = stringBuilder.toString();
        System.out.println(y);
    }
    //最大公约数
    public static int y(int a, int b){
        return b == 0 ? a :y( b,a % b);//辗转
    }

}
